﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using WebCrawlerTools.page;
using System.IO;

namespace WebCrawlerTools.data
{
    public class DataCenter
    {
        public static DataCenter Load()
        {
            DataCenter dataCenter = new DataCenter();
            dataCenter.BasicInfo = DBasicInfo.Load();
            return dataCenter;
        }

        public static void Store(DataCenter dataCenter)
        {
            DBasicInfo.Store(dataCenter.BasicInfo);
         }

        public static void ToCSV(ArrayList data,String csvFileName)
        {
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(csvFileName,true,Encoding.Default))
            {
                foreach(SLine row in data)
                {
                    writer.WriteLine(row.ToString());
                }
            }
        }

        public ArrayList HomePages
        {
            get { return BasicInfo.HomePages; }
            set { BasicInfo.HomePages = value; }
        }

         public DBasicInfo BasicInfo
        {
            get
            {
                return basicinfo;
            }
            set
            {
                basicinfo = value;
            }
        }

        public DDataInfo DataInfo
        {
            get
            {
                return datainfo;
            }
            set
            {
                datainfo = value;
            }
        }

        private DBasicInfo basicinfo;
        private DDataInfo datainfo = new DDataInfo();

        internal void StoreListPageData(string path)
        {

            try
            {
                StreamWriter writer = new StreamWriter(path, true, Encoding.Default);
                writer.WriteLine(datainfo.ListPageData.ToString());
                writer.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }

        public void StoreDetailPageData(string path)
        {

            try
            {
                StreamWriter writer = new StreamWriter(path, true, Encoding.Default);
                writer.WriteLine(datainfo.DetailPageData.ToString());
                writer.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }

        public void SaveDetailPageData(WebCrawlerTools.search.SResult DetailPageData)
        {
            try
            {
                if (!Directory.Exists(basicinfo.DataPath))
                {
                    Directory.CreateDirectory(basicinfo.DataPath);
                }
                string fileName = basicinfo.DataPath+"\\data.csv";
                bool appendFlg = true;
                if(!File.Exists(fileName))
                {
                    appendFlg = false;
                }
                StreamWriter writer = new StreamWriter(fileName, true, Encoding.Default);
                if (appendFlg)
                {
                    writer.Write(DetailPageData.DataToString());
                }
                else
                {
                    writer.Write(DetailPageData.ToString());
                }
                writer.Close();
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.StackTrace);
            }
        }
    }
}
